package com.guoyansoft.model.service.impl;

import com.guoyansoft.config.R;
import com.guoyansoft.model.dao.*;
import com.guoyansoft.model.pojo.Customer;
import com.guoyansoft.model.pojo.Goods;
import com.guoyansoft.model.pojo.HistoricalOrders;
import com.guoyansoft.model.pojo.StatisticalReport;
import com.guoyansoft.model.service.GoodsService;
import com.guoyansoft.model.service.HistoricalOrdersService;
import com.guoyansoft.model.service.PendingOrdersService;
import com.guoyansoft.model.service.StatisticalReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 刘耀诚
 * 历史订单记录service实现类
 */
@Service
public class HistoricalOrdersServiceImpl implements HistoricalOrdersService {
    @Autowired
    private HistoricalOrdersDao historicalOrdersDao;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private StatisticalReportService statisticalReportService;
    @Autowired
    private PendingOrdersDao pendingOrdersDao;
    @Autowired
    private CourierAndPendingOrdersDao courierAndPendingOrdersDao;
    @Autowired
    private CourierDao courierDao;
    @Autowired
    private CustomerDao customerDao;
    @Override
    public R save(Map<String,Object> params) {
        HistoricalOrders historicalOrders = (HistoricalOrders) params.get("historicalOrders");
        String customName = (String) params.get("customName");
        Customer customer = customerDao.selectByCustomerName(customName);
        String goodsNum = historicalOrders.getGoodsNum();
        Integer total=0;
        if(goodsNum.contains(",")){
            String[] goodsNums = goodsNum.split(",");
            for (int i = 0; i < goodsNums.length; i++) {
                String substring = goodsNums[i].substring(goodsNums[i].lastIndexOf("=") + 1);
                total+=Integer.parseInt(substring);
            }
            customer.setMonthUsage(customer.getMonthUsage()+total);
            customer.setRemark("已下单");
        }else {
            String substring = goodsNum.substring(goodsNum.lastIndexOf("=") + 1);
            total+=Integer.parseInt(substring);
            customer.setMonthUsage(customer.getMonthUsage()+total);
            customer.setRemark("已下单");
        }
        int row = historicalOrdersDao.insert(historicalOrders);
        Integer integer = courierDao.selectByName(historicalOrders.getCourier());
        Map<String,Object> map=new HashMap<>();
        map.put("courierId",integer);
        map.put("pendingordersId",historicalOrders.getNo());
        int delete = courierAndPendingOrdersDao.delete(map);
        int row2 = pendingOrdersDao.delete(new String[]{historicalOrders.getNo()});


        return row>0&&row2>0&&delete>0?R.suc():R.fail();
    }

    @Override
    public R saveRevoke(HistoricalOrders historicalOrders) {
        int row = historicalOrdersDao.insert(historicalOrders);
        int row1 = pendingOrdersDao.delete(new String[]{historicalOrders.getNo()});
        return row>0&&row1>0?R.suc():R.fail();
    }

    @Override
    public R modify(HistoricalOrders historicalOrders) {
        int row = historicalOrdersDao.update(historicalOrders);
        return row>0?R.suc():R.fail();
    }

    @Override
    public R drop(Integer[] id) {
        int row = historicalOrdersDao.delete(id);
        return row>0?R.suc():R.fail();
    }

    @Override
    public R queryByNo(String no) {
        HistoricalOrders historicalOrders = historicalOrdersDao.selectByNo(no);
        return R.suc(historicalOrders);
    }

    @Override
    public R queryByPage(Map<String, Object> params) {
        List<HistoricalOrders> historicalOrders = historicalOrdersDao.selectByPage(params);
        Long total = historicalOrdersDao.selectTotal(params);
        return R.suc(total,historicalOrders);
    }


    @Override
    public R queryByShopName(Map<String,Object> params) {
        List<HistoricalOrders> historicalOrders = historicalOrdersDao.selectByShopName(params);

        List<Goods> goodsList = new ArrayList<>();
        for (int i = 0; i < historicalOrders.size(); i++) {
            String goodsName = historicalOrders.get(i).getGoodsName();
            String goodsNum = historicalOrders.get(i).getGoodsNum();
            if (goodsName.contains(",") && goodsNum.contains(",")) {
                String[] goodsNames = goodsName.split(",");
                String[] goodsNums = goodsNum.split(",");
                for (int j = 0; j < goodsNums.length; j++) {
                    String substring = goodsNums[j].substring(goodsNums[j].lastIndexOf("=") + 1);
                    Goods goods = goodsService.queryByGoodsName(goodsNames[j]);
                    goods.setNum(Integer.parseInt(substring));
                    goodsList .add (goods);
                }
            }else {
                String substring = goodsNum.substring(goodsNum.lastIndexOf("=") + 1);
                Goods goods=goodsService.queryByGoodsName(goodsName);
                 goods. setNum(Integer.parseInt(substring));
                goodsList .add (goods);
            }
        }
        Map<String, Object> param = new HashMap<>();
        param.put("historicalOrdersKey", historicalOrders);
        param.put("goodsKey", goodsList);
        return R.suc(param);
    }

    @Override
    public R queryByShopNameYear(Map<String, Object> params) {
        List<HistoricalOrders> historicalOrders = historicalOrdersDao.selectByShopNameYear(params);
        List<Goods> goodsList = new ArrayList<>();
        for (int i = 0; i < historicalOrders.size(); i++) {
            String goodsName = historicalOrders.get(i).getGoodsName();
            String goodsNum = historicalOrders.get(i).getGoodsNum();
            if (goodsName.contains(",") && goodsNum.contains(",")) {
                String[] goodsNames = goodsName.split(",");
                String[] goodsNums = goodsNum.split(",");
                for (int j = 0; j < goodsNums.length; j++) {
                    String substring = goodsNums[j].substring(goodsNums[j].lastIndexOf("=") + 1);
                    Goods goods = goodsService.queryByGoodsName(goodsNames[j]);
                    goods.setNum(Integer.parseInt(substring));
                    goodsList .add (goods);
                }
            }else {
                String substring = goodsNum.substring(goodsNum.lastIndexOf("=") + 1);
                Goods goods=goodsService.queryByGoodsName(goodsName);
                goods. setNum(Integer.parseInt(substring));
                goodsList .add (goods);
            }
        }
        Map<String, Object> param = new HashMap<>();
        param.put("historicalOrdersKey", historicalOrders);
        param.put("goodsKey", goodsList);
        return R.suc(param);
    }
}
